//
//  RCUserVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-8-19.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCUserVC.h"
#import "RCUserVCCell.h"
#import "RCUserOdersListVC.h"
#import "RCFansListVC.h"
#import "RCUserScoreVC.h"
#import "RCOrderManagerVC.h"


#import "RCMemberCardVC.h"
#import "RCUserRegVC.h"
#import "RCFeedbackVC.h"
#import "RCTagsController.h"
#import "RCUserPhoneRegFullfilNickVC.h"
#import "RCUserEmailRegVC.h"
#import "GloableEngineService.h"
#import "RCMessageDetailDialogVC.h"
#import "RCUserDataModel.h"
#import "RCEditUserInfoVC.h"

#import "IManager+ModulePersonCenter.h"

@interface RCUserVC ()<RCUserProfileProtocol,IManagerDelegate>{
//    NSInteger   signinCount; //签到数
//    NSInteger   reviewCount; //点评数
//    NSInteger   photosCount; //照片数
    RCUserProfileModel *_model;
    int64_t     _reqID;
    int64_t     _followReq;
    
    NSString    *_userID;
    BOOL        isMySelf;
}
@property (nonatomic, retain) RCUserProfileModel *model;

@end

@implementation RCUserVC
@synthesize delegate = _delegate;
@synthesize model    = _model;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _reqID = kInvalidID;
        isMySelf = YES;
    }
    return self;
}


- (id) initWithUserID:(NSString *)userID
{
    self = [super init];
    if (self) {
        _userID = [userID copy];
        _reqID = kInvalidID;
        isMySelf = NO;

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    
    // 设置tableview相关参数
    if ( baseVCTableView != nil ) {
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
//        baseVCTableView.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.backgroundColor = [UIColor clearColor];
        baseVCTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        baseVCTableView.frame = self.view.bounds;
        CGRect tableViewNewFrame = CGRectMake(baseVCTableView.frame.origin.x,0,baseVCTableView.frame.size.width,UIScreenHeight-44-49-20-4);
        baseVCTableView.frame = tableViewNewFrame;
        baseVCTableView.tableHeaderView = nil;
        baseVCTableView.tableFooterView = nil;
//        [self addPullRefresh];
    }
    
    if (_reqID == kInvalidID) {
        _reqID = [[IManager sharedInstance] requestPersonInfo:_userID WithDelegte:self];
    }
    
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (_reqID == kInvalidID) {
        _reqID = [[IManager sharedInstance] requestPersonInfo:_userID WithDelegte:self];
    }

}

- (void) viewWillDisappear:(BOOL)animated
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    _reqID = kInvalidID;
}

- (void) dealloc
{
    [_userID release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UITableView Datasource & Delegate

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;

    }else{
        if (isMySelf) {
            return 6;
        }else{
            return 3;
    
        }

    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath section] == 0) {
        return 125;
    }else if ([indexPath section] == 1){
        return 10;
    }else{
        return 46;
    }
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath section] == 0) {
        RCUserProfileCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"RCUserProfileCell"];
        if (nil == headerCell) {
            headerCell = [[RCUserProfileCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCUserProfileCell"];
            headerCell.delegate = self;
        }
        headerCell.isMySelf = isMySelf;
        [headerCell bindData:self.model];
        return headerCell;
    }else if ([indexPath section] == 1){
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        return [cell autorelease];
    }else{
        
        RCUserInteractCell *interAct =  [tableView dequeueReusableCellWithIdentifier:@"RCUserInteractCell"];
        if (interAct == nil) {
            interAct = [[[RCUserInteractCell alloc] init] autorelease];

        }
        if (isMySelf) {
            if ([indexPath row] == 5) {
                interAct.isLast = YES;
            }else{
                interAct.isLast = NO;
            }
        }else{
            if ([indexPath row] == 2) {
                interAct.isLast = YES;
            }else{
                interAct.isLast = NO;
            }
        }
        if (isMySelf) {
            [interAct bindData:[indexPath row]];
        }else{
            if ([indexPath row] == 0) {
                [interAct bindData:1];
            }else{
                [interAct bindData:[indexPath row]+3];
            }
        }

        return interAct;
    }
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath section] == 2) {
        if (isMySelf) {
            if ([indexPath row] == 0) {
                //我的订单
                [self btnClickToListVC:FansListVCType_myOrders];
            }else if ([indexPath row] == 1){
                //我的收藏
                [self btnClickToListVC:FansListVCType_myCollect];
            }else if ([indexPath row] == 2){
                //我的积分
                RCUserScoreVC *vc = [[RCUserScoreVC alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                
                [self.parentViewController.navigationController pushViewController:vc animated:YES];
                [vc release];
            }else if ([indexPath row] == 3){
                //留言
                RCFeedbackVC *feedbackVC = [[RCFeedbackVC alloc]init];
                feedbackVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:feedbackVC animated:YES];
                [feedbackVC release];
            }else if ([indexPath row] == 4){
                //我关注的人
                [self btnClickToListVC:FansListVCType_follow];
                
                
            }else if ([indexPath row] == 5){
                //我的粉丝列表
                [self btnClickToListVC:FansListVCType_fans];
            }
        }else{
            if ([indexPath row] == 0) {
                //他的收藏
                [self btnClickToListVC:FansListVCType_myCollect];
            }else if ([indexPath row] == 1){
                //他关注的人
                [self btnClickToListVC:FansListVCType_follow];
            }else if ([indexPath row] == 2){
                //他的粉丝列表
                [self btnClickToListVC:FansListVCType_fans];
            }
        }
//        }else if ([indexPath row] == 6){
//            //领取会员卡
//            RCMemberCardVC *vc = [[RCMemberCardVC alloc] init];
//            vc.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:vc animated:YES];
//            [vc release];
//        }else if([indexPath row] == 7){
//            //领取会员卡
//            RCUserRegVC *vc = [[RCUserRegVC alloc] init];
//            vc.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:vc animated:YES];
//            [vc release];
//        }else{
//            
//           
//        }
    
    }
}


#pragma mark 签到、点评、照片被点击
- (void) btnClickToListVC:(FansListVCType) listType
{
    if (listType == FansListVCType_mailBox) {
        if (isMySelf) {
            RCOrderManagerVC *vc = [[RCOrderManagerVC alloc] initWithVCType:RCListType_MessageManage withUniqueID:nil];
            vc.hidesBottomBarWhenPushed = YES;
            [self.parentViewController.navigationController pushViewController:vc animated:YES];
            [vc release];
            return;
        }else{
            RCMessageDetailDialogVC *vc = [[RCMessageDetailDialogVC alloc] initWithUserName:self.model.userName];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            [vc release];
            return;
        }
        
    }
    
    RCFansListVC *vc = [[RCFansListVC alloc] initWithUserID:_userID];
    vc.vcType = listType;
    vc.hidesBottomBarWhenPushed = YES;
    if (isMySelf) {
        [self.parentViewController.navigationController pushViewController:vc animated:YES];
    }else{
        [self.navigationController pushViewController:vc animated:YES];
    }
    [vc release];
    
}


- (void) followOrCancelSomebody
{
    NSInteger statusInt = 1;
    if (self.model.hasFocused) {
        statusInt = 2;
    }else{
        statusInt = 1;
    }
    _reqID = [[IManager sharedInstance] requestFollowOrCancel:_userID withStatus:statusInt WithDelegte:self];
}


- (void) editMyProfile
{
    //我的积分
    RCEditUserInfoVC *vc = [[RCEditUserInfoVC alloc] initWithNickName:self.model.userName liveCity:self.model.liveCity liveCityID:self.model.liveCityID isMan:self.model.isMan];
    vc.hidesBottomBarWhenPushed = YES;
    
    [self.parentViewController.navigationController pushViewController:vc animated:YES];
    [vc release];
    
}


#pragma mark Imanager Delegate

- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if ([dataModel isKindOfClass:[RCUserProfileModel class]]) {
        if (dataModel.errorNO == RCErrorTypeNoError) {
            self.model = (RCUserProfileModel *)dataModel;
        }else{
            [_delegate logoutSuccess];
        }
    }else{
        if (dataModel.errorNO == RCErrorTypeNoError) {
            self.model.hasFocused = !self.model.hasFocused;
        }
    }
    
    [baseVCTableView reloadData];

}
@end
